﻿using System.Windows;
using System.Windows.Controls;

namespace MetroWPF.Controls
{
    /// <summary>
    /// Represents a control that can be used to display or edit unformatted text.
    /// </summary>
    public class TextBox : System.Windows.Controls.TextBox
    {
        /// <summary>
        /// Identifies the IsNull dependency property.
        /// </summary>
        public static readonly DependencyProperty IsNullProperty =
            DependencyProperty.Register("IsNull", typeof(bool), typeof(TextBox), new PropertyMetadata(true));

        /// <summary>
        /// Identifies the WaterMark dependency property.
        /// </summary>
        public static readonly DependencyProperty WaterMarkProperty =
            DependencyProperty.Register("WaterMark", typeof(string), typeof(TextBox), new PropertyMetadata(default(string)));

        /// <summary>
        /// Name of the delete button in control template.
        /// </summary>
        internal const string DeleteButtonTemplateName = "PART_DeleteButton";

        /// <summary>
        /// The delete button in control template.
        /// </summary>
        private Button _deleteButton;

        /// <summary>
        /// Gets or sets a value that indicates whether the text is null in the control.
        /// </summary>
        public bool IsNull
        {
            get { return (bool)GetValue(IsNullProperty); }
            private set { SetValue(IsNullProperty, value); }
        }

        /// <summary>
        /// Gets or sets a value that indicates the water mark in the control.
        /// </summary>
        public string WaterMark
        {
            get { return (string)GetValue(WaterMarkProperty); }
            set { SetValue(WaterMarkProperty, value); }
        }

        /// <summary>
        /// Is called when a control template is applied.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            InitializeDeleteButton();
        }

        /// <summary>
        /// Is called when content in this editing control changes.
        /// </summary>
        /// <param name="e">The arguments that are associated with the System.Windows.Controls.Primitives.TextBoxBase.TextChanged</param>
        protected override void OnTextChanged(TextChangedEventArgs e)
        {
            base.OnTextChanged(e);
            this.IsNull = string.IsNullOrEmpty(this.Text);
        }

        /// <summary>
        /// Initialize the delete button in a textbox.
        /// </summary>
        private void InitializeDeleteButton()
        {
            //Get the delete button in the template.
            this._deleteButton = base.GetTemplateChild(DeleteButtonTemplateName) as Button;
            if (this._deleteButton != null)
            {
                this._deleteButton.Click += OnDeleteButtonClick;
            }
        }

        /// <summary>
        /// Is called when the delete button is clicked.
        /// </summary>
        /// <param name="sender">The caller (the delete button).</param>
        /// <param name="e">EventArgs of the click event of the button.</param>
        private void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            this.Clear();
        }
    }
}